package Grade;

import seqList.SeqList;

import java.util.Scanner;

public class StudentSqList extends SeqList {
    public StudentSqList(int maxSize, int n){
        super(maxSize);
        Scanner sc=new Scanner(System.in);
        for (int i = 1; i <=n; i++) {                //创建含有n个数据元素的顺序表
            StudentNode node=new StudentNode(sc);
            if(node!=null){
                insert(node);            //新元素插入
            }
            else
                i--;                    //如果不成功，就不计数
        }
    }

    public void insert(StudentNode node) {
        super.insert(this.length(),node);           //表尾插入信息
    }

    //覆盖父类get方法
    public StudentNode get(int number) throws Exception {
        for (int i = 0; i < length(); i++) {        //遍历顺序表
            StudentNode node = (StudentNode) super.get(i);        //调用父类get方法
            if (node.number == number) {
                return node;                        //返回学生信息
            }
        }
        throw new Exception("学号"+number+"不存在");   //编号不服 抛出异常
    }

    public Object remove(int number) throws Exception {
        for (int i = 0; i < length(); i++) {
            StudentNode node=(StudentNode) super.get(i);
            if(node.number==number){
                super.remove(i);
                return null;
            }
        }
        throw new Exception("删除位置不合法");

    }

    public void display(){
        for (int i = 0; i < length(); i++) {
            try {
                   StudentNode node=(StudentNode) super.get(i); //遍历顺序表
                   displayNode(node);
            }catch (Exception e){}

        }
    }

    public void displayNode(StudentNode node) {
        System.out.println("序号:"+node.ranking+"  学号："+node.number+"  姓名:"+ node.name+"  性别:"+ node.sex+"  数学成绩:"+ node.math+"  英语成绩:"
                +node.english+"  语文成绩:"+ node.chinese+"  总分:"+node.sum);
    }

    public void sort() throws Exception {
        for (int i = 0; i < length()-1  ; i++) {           // 冒泡排序
            for (int j = 0; j <length()-1-i ; j++) {
                StudentNode node1=(StudentNode) super.get(j);
                StudentNode node2=(StudentNode) super.get(j+1);
                    if (node1.sum<node2.sum){
                       StudentNode temp=(StudentNode) node1 ;
                        set(j,node2);
                        set(j+1,temp);
                        System.out.println("排序之后的顺序表：");
                    }
            }
        }
        for (int i = 0; i < length(); i++) {
            StudentNode node1=(StudentNode) super.get(i);
            StudentNode node2=(StudentNode) super.get(i+1);
            node1.ranking=i+1;
            if (node1.sum==node2.sum)
            node1.ranking=node2.ranking=i+1;

        }
        System.out.println();
         display();
        }
    }

